package com.zhangyj.impl.actions;

import com.zhangyj.BoastException;
import com.zhangyj.action.AbstractAction;
import com.zhangyj.action.RequestAction;
import com.zhangyj.action.RequestMethod;
import com.zhangyj.dao.DaoManager;
import com.zhangyj.dao.RoleDao;
import com.zhangyj.entitis.User;
import com.zhangyj.impl.ViewName;
@RequestAction(name="user")
public class UserAction extends AbstractAction {
	private static final long serialVersionUID = -6621573079756672368L;


	@RequestMethod(name="list")
	public String list() throws Exception {return null;/*
		String contextPath = request.getContextPath();
		SystemParam systemParam = SystemParam.getSystemParam(request);
		Pagination pagination = new Pagination(request);
		String yhm = request.getParameter("yhm");
		UserDao userdao = DaoManager.getDao(UserDao.class);
		pagination.setTotalCount(userdao.getUserCount(yhm));

		List<Object[]> mlist = new ArrayList<Object[]>();
		List<User> list = userdao.getUserList(pagination, yhm);

		for (Iterator<User> iterator = list.iterator(); iterator.hasNext();) {
			User user = iterator.next();
			Object[] arr = new Object[8];
			arr[0] = user.getNickName();
			arr[1] = user.getCode();
			arr[2] = user.getLgnName();
			arr[3] = new HyperLink(contextPath + "/user/view.do?" + systemParam.toUrlParam() + "&userid=", String.valueOf(user.getId()), LocalMessage.get("view"), "_self");
			arr[4] = new HyperLink(contextPath + "/user/edit.do?" + systemParam.toUrlParam() + "&userid=", String.valueOf(user.getId()), LocalMessage.get("modify"), "_self");
			arr[5] = new HyperLink(contextPath + "/user/del.do?" + systemParam.toUrlParam() + "&userid=", String.valueOf(user.getId()), LocalMessage.get("del"), "_self");
			arr[6] = new HyperLink(contextPath + "/user/pwsrst.do?" + systemParam.toUrlParam() + "&userid=", String.valueOf(user.getId()), LocalMessage.get("resetpassword"), "_self");
			arr[7] = new HyperLink(contextPath + "/user/role.do?" + systemParam.toUrlParam() + "&userid=", String.valueOf(user.getId()), LocalMessage.get("user.set.role"), "_self");
			mlist.add(arr);
		}
		GridCol[] head = new GridCol[8];
		head[0] = new GridCol("用户名", 0);
		head[1] = new GridCol("编号", 0);
		head[2] = new GridCol("登录名", 0);
		head[3] = new GridCol(LocalMessage.get("view"));
		head[4] = new GridCol(LocalMessage.get("modify"));
		head[5] = new GridCol(LocalMessage.get("del"));
		head[6] = new GridCol(LocalMessage.get("resetpassword"), 100);
		head[7] = new GridCol(LocalMessage.get("user.set.role"), 100);
		ITableModel tm = new DefaultTableMode(mlist.iterator(), head);
		HTMLTable ht = new DefaultHTMLTable(tm);
		ht.setDefaultRenderer(HyperLink.class, new HyperLinkCellRender());
		request.setAttribute("table", ht);
		StringBuilder url = new StringBuilder();
		url.append("/user/list.do");
		if (yhm != null) {
			url.append("?yhm=").append(yhm);
		}
		pagination.setUrl(url.toString());
		return ViewName.USER_LIST;
	*/}

	public String newUser() throws Exception {
		User user = new User();
		user.setCode("");
		user.setLgnName("");
		user.setNickName("");
		request.setAttribute("user", user);
		return ViewName.USER_NEW;
	}

	public String edit() throws Exception {return null;/*
		String userid = request.getParameter("userid");
		int id = -1;
		try {
			id = Integer.parseInt(userid);
		} catch (Exception e) {
			throw new BoastException("传入的参数不正确！");
		}
		User user = DaoManager.getDao(UserDao.class).load(id);
		request.setAttribute("user", user);
		return ViewName.USER_EDIT;
	*/}
	@RequestMethod(name="view")
	public String view() throws Exception {
		edit();
		return ViewName.USER_VIEW;
	}
	@RequestMethod(name="del")
	public String del() throws Exception {return null;/*
		String userid = request.getParameter("userid");
		int id = -1;
		try {
			id = Integer.parseInt(userid);
		} catch (Exception e) {
			throw new BoastException("传入的参数不正确！");
		}
		DaoManager.getDao(UserDao.class).del(id);
		return list();
	*/}
	@RequestMethod(name="resetpwd")
	public String resetpwd() throws Exception {
		edit();
		return ViewName.USER_PSWORDRESET;
	}
	@RequestMethod(name="save")
	public String save() throws Exception {return null;/*
		UserDao dao = DaoManager.getDao(UserDao.class);
		String userid = request.getParameter("userid");
		String model = request.getParameter("model");
		int id = -1;
		try {
			id = Integer.parseInt(userid);
		} catch (Exception e) {

		}
		User user = null;
		if (id == -1) {
			user = new User();
			user.setId(DBUtil.getIntId("user"));
		} else {
			user = dao.load(id);
		}
		String nickName = getParameter( "username");
		if (nickName.length() > 0) {
			user.setNickName(nickName);
		}
		String code = getParameter( "code");
		if (code.length() > 0) {
			user.setCode(code);
		}
		String loginname = getParameter( "loginname");
		if (loginname.length() > 0) {
			user.setLgnName(loginname);
		}
		String psword = getParameter( "psword");
		if (psword.length() > 0) {
			psword = AlgorithmUtil.md5Digest(AlgorithmUtil.desEncrypt(psword.getBytes(), AlgorithmUtil.fieldDesKey));
			user.setPassword(psword);
		}
		if (id == -1) {
			dao.save(user, false);
		} else {
			dao.save(user, true);
		}
		request.setAttribute("user", user);
		if ("edit".equals(model)) {
			return ViewName.USER_EDIT;
		} else if ("new".equals(model)) {
			return ViewName.USER_NEW;
		} else if ("pswordreset".equals(model)) {
			return ViewName.USER_PSWORDRESET;
		}
		return list();
	*/}

	public String role() throws Exception {return null;/*
		String userid = request.getParameter("userid");
		int id = -1;
		try {
			id = Integer.parseInt(userid);
		} catch (Exception e) {
			throw new BoastException("传入的参数不正确！");
		}
		User user = DaoManager.getDao(UserDao.class).load(id);
		RoleDao roleDao = DaoManager.getDao(RoleDao.class);
		List<?> roleList = roleDao.getList();
		Set<Integer> userRole = roleDao.getUserRole(id);
		List<Object[]> mlist = new ArrayList<Object[]>();
		for (Iterator<?> iterator = roleList.iterator(); iterator.hasNext();) {
			Role role = (Role) iterator.next();
			Object[] arr = new Object[3];

			arr[0] = new CheckBox("roles", String.valueOf(role.getRoleid()), userRole.contains(role.getRoleid()));
			arr[1] = role.getRolename();
			arr[2] = role.getRemark();
			mlist.add(arr);
		}
		ITableModel tm = new DefaultTableMode(mlist.iterator(), new GridCol[] { new GridCol("选择状态", 100), new GridCol("角色名称", 200), new GridCol("备注", 0) });
		HTMLTable ht = new DefaultHTMLTable(tm);
		request.setAttribute("user", user);
		request.setAttribute("table", ht);
		return ViewName.USER_ROLE;
	*/}
	@RequestMethod(name="saveRole")
	public String saveRole() throws Exception {
		String userid = request.getParameter("userid");
		String[] roles = request.getParameterValues("roles");
		if (roles == null) {
			roles = new String[] {};
		}
		int id = -1;
		int roleids[] = new int[roles.length];
		try {
			id = Integer.parseInt(userid);
			for (int i = 0; i < roles.length; i++) {
				roleids[i] = Integer.valueOf(roles[i]);
			}
		} catch (Exception e) {
			throw new BoastException("传入的参数不正确！");
		}
		RoleDao roleDao = DaoManager.getDao(RoleDao.class);
		roleDao.setUserRole(id, roleids);
		return role();
	}

}
